var validator;
var $deptAddForm = $("#dept-add-form");

$(function () {
    validateRule();
    createDeptTree();
    createMeetingRoomTree();
    createMenuTree();

    $("#dept-add .btn-save").click(function () {
        var name = $(this).attr("name");
        getDept();
        getMeetingRoom();
        getMenu();
        validator = $deptAddForm.validate();
        var flag = validator.form();
        //console.log($deptAddForm.serialize());
        if (flag) {
            if (name === "save") {
                $.post(ctx + "dept/add", $deptAddForm.serialize(), function (r) {
                    if (r.code === 0) {
                        closeModal();
                        refresh();
                        $MB.n_success(r.msg);
                    } else $MB.n_danger(r.msg);
                });
            }
            if (name === "update") {
                $.post(ctx + "dept/update", $deptAddForm.serialize(), function (r) {
                    if (r.code === 0) {
                        closeModal();
                        refresh();
                        $MB.n_success(r.msg);
                    } else $MB.n_danger(r.msg);
                });
            }
        }
    });

    $("#dept-add .btn-close").click(function () {
        closeModal();
    });

});

function closeModal() {
    $("#dept-add-button").attr("name", "save");
    $("#dept-add-modal-title").html($MB.i18n('dept.add'));
    validator.resetForm();
    $MB.closeAndRestModal("dept-add");
    $MB.refreshJsTree("deptTree", createDeptTree());
    $MB.resetJsTree("meetingRoomTree");
    $MB.resetJsTree("menuTree");
}

function validateRule() {
    var vali_dept_msg1 = $MB.i18n('vali.dept.msg1'),
        vali_dept_msg2 = $MB.i18n('vali.dept.msg2'),
        vali_dept_msg3 = $MB.i18n('vali.dept.msg3');
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    validator = $deptAddForm.validate({
        rules: {
            deptName: {
                required: true,
                minlength: 3,
                maxlength: 15,
                remote: {
                    url: "dept/checkDeptName",
                    type: "get",
                    dataType: "json",
                    data: {
                        deptName: function () {
                            return $("input[name='deptName']").val().trim();
                        },
                        oldDeptName: function () {
                            return $("input[name='oldDeptName']").val().trim();
                        }
                    }
                }
            },
            parentId: {
            	required: true
            }
        },
        messages: {
            deptName: {
                required: icon + vali_dept_msg1,
                minlength: icon + vali_dept_msg2,
                remote: icon + vali_dept_msg3
            },
            parentId: icon + $MB.i18n('vali.dept.parentId.msg')
        }
    });
}

function createDeptTree() {
    $.post(ctx + "dept/tree?_="+new Date().getTime(), {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#deptTree').jstree({
                "core": {
                    'data': data.children,
                    'multiple': false
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $MB.n_danger(r.msg);
        }
    })

}

function createMeetingRoomTree(){
	$.post(ctx + "meetingroom/tree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#meetingRoomTree').jstree({
                "core": {
                    'data': data
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": true
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $MB.n_danger(r.msg);
        }
    })
}

function createMenuTree() {
    $.post(ctx + "menu/getUserMenu", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#menuTree').jstree({
                "core": {
                    'data': data
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": true
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $MB.n_danger(r.msg);
        }
    })
}

function getDept() {
    var ref = $('#deptTree').jstree(true);
    var deptIds = ref.get_checked();
    $("[name='parentId']").val(deptIds[0]);
}

function getMeetingRoom() {
    var $meetingRoomTree = $('#meetingRoomTree');
    var ref = $meetingRoomTree.jstree(true);
    var meetingRoomIds = ref.get_checked();
    $meetingRoomTree.find(".jstree-undetermined").each(function (i, element) {
    	meetingRoomIds.push($(element).closest('.jstree-node').attr("id"));
    });
    $("[name='meetingRoomId']").val(meetingRoomIds);
    console.log($("[name='meetingRoomId']").val());
}

function getMenu() {
	var $menuTree = $('#menuTree');
    var ref = $menuTree.jstree(true);
    var menuIds = ref.get_checked();
    $menuTree.find(".jstree-undetermined").each(function (i, element) {
        menuIds.push($(element).closest('.jstree-node').attr("id"));
    });
    $("[name='menuId']").val(menuIds);
    console.log($("[name='menuId']").val());
}